#!/usr/bin/env python

from mxit import handles
from mxit import encryption
from mxit import errors
from blend import constants
import gettext

gettext.install('blendIT','locale');

class listener:

   oldP = None;

   def __init__(self,blend):
      self.blend = blend;

   def status_changed(self,status,reason):
      if status == handles.Status.DISCONNECTED:
         self.blend.gui.buddyWindow.rememberExpands();
         self.blend.gui.buddyWindow.clear();
         widget = self.blend.gui.buddyWindow.statusBox;
         handler = self.blend.gui.buddyWindow.statusBoxChangeHandler;
         
         if reason != handles.StatusChangeReason.REQUESTED:
            model = widget.get_model();
            iter = widget.get_active_iter();
            try:
               self.oldP = constants.C.presences[handles.Presence.byid(model.get_value(iter,2))];
            except:
               self.oldP = None;
         else:
            # Requested. Make mood 'None' again
            hand = self.blend.gui.buddyWindow.moodBoxChangeHandler;
            wid = self.blend.gui.buddyWindow.moodBox;
            wid.handler_block(hand);
            wid.set_active(0);
            wid.handler_unblock(hand);
         
         p = constants.C.presences[handles.Presence.OFFLINE];
         pres = constants.C.orderedPresences.index(p);
         widget.handler_block(handler);
         widget.set_active(pres);
         widget.handler_unblock(handler);
         self.blend.gui.buddyWindow.changeMenuToLogOn();
         self.blend.gui.buddyWindow.tree.set_sensitive(False);
      elif status == handles.Status.AUTHENTICATING:
         self.blend.gui.buddyWindow.changeConnectingStatus(_("Logging In..."));
      elif status == handles.Status.ACTIVE:
         widget = self.blend.gui.buddyWindow.statusBox;
         handler = self.blend.gui.buddyWindow.statusBoxChangeHandler;
         widget.handler_block(handler);
         self.blend.gui.buddyWindow.removeTempStatus();
         self.blend.gui.buddyWindow.changeMenuToLogOff();
         widget.handler_unblock(handler);
         
         # We must resend our presence
         if self.oldP is not None:
            pres = constants.C.orderedPresences.index(self.oldP);
            widget.set_active(pres);
            self.oldP = None;
         
         # We must resend our mood
         widget = self.blend.gui.buddyWindow.moodBox;
         widget.emit('changed');
         
         self.blend.gui.buddyWindow.tree.set_sensitive(True);
      elif status == handles.Status.CONNECTING:
         if not self.blend.currentlyConnected:
            self.blend.gui.buddyWindow.addConnectingStatus(_("Connecting..."));
      #print "bW sc";

   # MultiMx
   def room_added(self,room,message=None,inviter=None):
      self.blend.gui.chatWindow.createNewChat(room,True,self.blend.gui.chatWindow.drawChatPage);

      if inviter is None:
         inviter = _("Unknown");
        
      self.blend.gui.buddyWindow.addMultiMx(room,str(inviter));


   def room_removed(self,room):      
      self.buddy_removed(room);


   # Regular Buddies
   def buddy_updated(self,buddy,presence=None,mood=None,name=None,group=None,old_presence=None):
      if mood != None or presence != None:
         iter = self.blend.gui.buddyWindow.findBuddy(buddy);
         if iter != -1:
            self.blend.gui.buddyWindow.updateBuddy(iter,buddy);
            if old_presence == handles.Presence.PENDING:
               c = constants.C.types[buddy.type].int;
               self.blend.gui.buddyWindow.model.get_model().set_value(iter,5,c);
         else:
            # Buddy doesn't exist in the window. Weird
            pass;
      else:
         if buddy.presence == handles.Presence.PENDING:
            self.blend.gui.buddyWindow.addInvite(buddy);
         else:
            iter = self.blend.gui.buddyWindow.findBuddy(buddy);
            if iter != -1:
               self.blend.gui.buddyWindow.updateBuddy(iter,buddy);

      self.blend.gui.buddyWindow.updateGroup(group=buddy.group);


   def buddy_added(self,buddy):
      if buddy.type in (handles.BuddyType.MXIT,handles.BuddyType.JABBER):
         # Buddy
         if buddy.is_subscribed() == True:
            iter = self.blend.gui.buddyWindow.findGroup(buddy.group);
            newG = False;

            if buddy.group.name == u"":
               newG = True;

            if iter == -1:
               # The group doesn't yet exist. So we must create it.
               iter = self.blend.gui.buddyWindow.addGroup(buddy.group);
               newG = True;
            self.blend.gui.buddyWindow.addBuddy(iter,buddy);
            if newG:
               self.blend.gui.buddyWindow.recallExpand(buddy.group);
               # Get rid of expands
               try:
                  del self.blend.gui.buddyWindow.expands[buddy.group.real_name];
               except KeyError:
                  pass;
         else:
            self.blend.gui.buddyWindow.addInvite(buddy);
            # Expand BlendIt group
            self.blend.gui.buddyWindow.recallExpand(buddy.group,True);
      else:
         # MXit Service
         buddy.group = self.blend.conn.roster.get_group("MXit Services");
         self.blend.gui.buddyWindow.addService(buddy);
         self.blend.gui.buddyWindow.recallExpand(buddy.group);
         # Get rid of expands
         try:
            del self.blend.gui.buddyWindow.expands[buddy.group.real_name];
         except KeyError:
            pass;

   # Buddy was removed...
   def buddy_removed(self,buddy):     
      iter = self.blend.gui.buddyWindow.findBuddy(buddy);

      if iter is not None:
         # Destroy this iter
         self.blend.gui.buddyWindow.model.get_model().remove(iter);